<?php

class AvisoService extends Model {

    /**
     * @var AvisoDao
     */
    private $avisoDao;
    /**
     * @var UsuarioService
     */
    private $usuarioService;
    /**
     * @var SetorService
     */
    private $setorService;

    public function injectDependencies() {
        $this->avisoDao = AvisoDao::getInstance();
        $this->usuarioService = UsuarioService::getInstance();
        $this->setorService = SetorService::getInstance();
    }

    public function findVisibleNoticeFromLoggedUser() {
        $ArNotices = $this->avisoDao->findByUser(Dracosys::getLoggedUser());
        $this->fillNotice($ArNotices);
        return $ArNotices;
    }

    public function findById($IDAviso) {
        $Aviso = $this->avisoDao->findById($IDAviso);
        $this->fillNotice($Aviso);
        return $Aviso;
    }

    public function findAll() {
        $ArAvisos = $this->avisoDao->findAll();
        $this->fillNotice($ArAvisos);
        return $ArAvisos;
    }

    public function findActiveNotices() {
        $ArAvisos = $this->avisoDao->findActiveNotices();
        $this->fillNotice($ArAvisos);
        return $ArAvisos;
    }

    public function findExperedNotices() {
        $ArAvisos = $this->avisoDao->findExperedNotices();
        $this->fillNotice($ArAvisos);
        return $ArAvisos;
    }

    public function findFutureNotices() {
        $ArAvisos = $this->avisoDao->findFutureNotices();
        $this->fillNotice($ArAvisos);
        return $ArAvisos;
    }

    public function save($ArDados) {
        $ArDados['IDSetor'] = $ArDados['IDSetor'] != 'TODOS' ? $ArDados['IDSetor'] : '';
        if( $this->avisoDao->save($ArDados) ) {
            $this->usuarioService->reload();
            return true;
        }
        return false;
    }

    public function update($ArDados) {
        $ArDados['IDSetor'] = $ArDados['IDSetor'] != 'TODOS' ? $ArDados['IDSetor'] : '';
        if( $this->avisoDao->update($ArDados) ) {
            $this->usuarioService->reload();
            return true;
        }
        return false;
    }

    private function fillNotice(&$MxNotice) {
        if (!empty($MxNotice)) {
            $this->fillNoticeWithUser($MxNotice);
            $this->fillNoticeWithSector($MxNotice);
        }
    }

    private function fillNoticeWithUser(&$MxNotice) {
        if (!is_array($MxNotice)) {
            $MxNotice->setUsuario($this->usuarioService->findSimpleUserById($MxNotice->getIDUsuario()));
            return;
        }
        foreach ($MxNotice as &$Notice) {
            $Notice->setUsuario($this->usuarioService->findSimpleUserById($Notice->getIDUsuario()));
        }
    }

    private function fillNoticeWithSector(&$MxNotice) {
        if (!is_array($MxNotice)) {
            if( !$MxNotice->isPublic() ) {
                $MxNotice->setSetor($this->setorService->findById($MxNotice->getIDSetor()));
            }
            return;
        }
        foreach ($MxNotice as &$Notice) {
            if( !$Notice->isPublic() ) {
                $Notice->setSetor($this->setorService->findById($Notice->getIDSetor()));
            }
        }
    }

    public function findNoticeByFilter($IDFilter, $filterType) {
        $ArNotices = array();
        if( $IDFilter == 'list-active' ) {
            $ArNotices = $this->findActiveNotices();
        } else if( $IDFilter == 'list-past' ) {
            $ArNotices = $this->findExperedNotices();
        } else if( $IDFilter == 'list-future' ) {
            $ArNotices = $this->findFutureNotices();
        } else if( $IDFilter == 'list-all' ) {
            $ArNotices = $this->findAll();
        }
        return $ArNotices;
    }

}

?>
